package com.zsy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zsy.mapper.ClazzMapper;
import com.zsy.pojo.Clazz;
import com.zsy.pojo.PageResult;
import com.zsy.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: zhengsuyang
 * @Desc:
 * @create: 2025-07-30 18:14
 **/
@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    /**
     * 添加班级
     * @param clazz
     */
    @Override
    public void addClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.addClazz(clazz);
    }



    @Override
    public PageResult<Clazz> getClazz(Integer page, Integer pageSize, String name, LocalDate begin, LocalDate end) {
        LocalDate localDate = LocalDate.now();
        // 1.获取分页
        PageHelper.startPage(page,pageSize);
        // 2.执行查询
        List<Clazz> clazz = clazzMapper.getClazz(name,begin,end);
        // 判断是否开班级
        for (Clazz clazz1 : clazz) {
            clazz1.setStatus("未开班");
            if (clazz1.getEndDate().isBefore(localDate)) {
                clazz1.setStatus("已结束");
            } else if (clazz1.getEndDate().isAfter(localDate)) {
                clazz1.setStatus("已开班");
            }
        }
        // 3.封装对象
        PageInfo<Clazz> pageInfo = new PageInfo<>(clazz);
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public Clazz getClazzById(Integer id) {
       Clazz clazz = clazzMapper.getClazzById(id);
       return clazz;
    }

    /**
     * 查询班级列表
     *
     * @return
     */
    @Override
    public List<Clazz> getClazzList() {
        List<Clazz> clazz = clazzMapper.getClazzList();
      return clazz;
    }

    /**
     * 修改班级
     *
     * @param clazz
     */
    @Override
    public void updateClazzById(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateClazzById(clazz);
    }

    /**
     * 根据id删除班级
     *
     * @param id
     * @return
     */
    @Override
    public Integer delClazzById(Integer id) {
        Integer count =  clazzMapper.queryClazzStu(id);
        if (count==0){
            clazzMapper.delClazzById(id);
            return 0;
        }
        else
            return count;
    }

}
